package org.hospital.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.hospital.entity.Bill;
import org.hospital.entity.Clinic;
import org.hospital.entity.Department;
import org.hospital.entity.Employee;
import org.hospital.entity.Project;
import org.hospital.service.BillService;
import org.hospital.service.ClinicService;
import org.hospital.service.DepartmentService;
import org.hospital.service.LogService;
import org.hospital.service.PermissionService;
import org.hospital.service.ProjectService;
import org.hospital.service.ReportService;
import org.hospital.util.Constant;
import org.hospital.util.DateUtil;
import org.hospital.util.DoubleUtil;
import org.hospital.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.WebRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("reportController")
@SessionAttributes(value = "employee", types = Employee.class)
public class ReportController {

	@Autowired
	private BillService bService;
	@Autowired
	private ProjectService pjService;
	@Autowired
	private ClinicService cService;
	@Autowired
	private DepartmentService dpmService;
	@Autowired
	private PermissionService pmService;
	@Autowired
	private LogService lService;
	@Autowired
	private ReportService rpService;

	@ResponseBody
	@RequestMapping(value = "/getIndexReport", produces = "text/json;charset=utf-8")
	public String getIndexReport(WebRequest req, HttpSession session) {
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		
		try {
			JSONObject resultJO = new JSONObject();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Clinic clinic = cService.getByid(operator.getClinicId());
			resultJO = getIndexReportResult(clinic, sdf.format(new Date()));
			return StringUtil.setResult(200, "获取成功", resultJO);
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/getClinicTreatmentReport_new", produces = "text/json;charset=utf-8")
	public String getClinicTreatmentReport_new(WebRequest req, HttpSession session) {
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(operator.getPositionId(), Constant.GET_REPORT.getName(), 3)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		
		try {
			int clinicId = Integer.parseInt(req.getParameter("clinicId"),10);
			int type = Integer.parseInt(req.getParameter("type"), 10);
			long datetime = DateUtil.getShortDateLong(req.getParameter("datetime"));
			
			
			JSONArray resultJA = rpService.getClinicTreatReport(clinicId, type, datetime);
			return StringUtil.setResult(200, "获取成功", resultJA);
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数有误", "");
		}
		
	}
	
	
	/**
	 * 获取门诊治疗项目汇总报表
	 * 
	 * @param req
	 *            {clinicId:xx,startTime:yyyy-MM-dd,endTime:yyyy-MM-dd}
	 * @param session
	 * @return 402:权限不足；405:重新登陆;200:成功
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/getClinicTreatmentReport", produces = "text/json;charset=utf-8")
	public String getClinicTreatmentReport(WebRequest req, HttpSession session) {
		
		Employee e = (Employee) session.getAttribute("employee");
		if (e == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(e.getPositionId(), Constant.GET_REPORT.getName(), 3)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		
		// 获取数据
		int clinicId = Integer.parseInt(req.getParameter("clinicId"), 10);
		Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
		Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));
		// TODO 验证数据合法性
		
		// 获取门诊[stratTime,endTime]内开的治疗项目
		Clinic clinic = cService.getByid(clinicId);
		List<Project> pjList = pjService.getClinicTreatmentProject(clinic, startTime, endTime);
		System.out.println("getClinicTreatmentReport.pjList : "+JSON.toJSONString(pjList));
		
		JSONArray resultJA = bService.getClinicTreatmentProjectReport(pjList, clinic, startTime,
				endTime);

		// 记录日志
		JSONObject logJO = new JSONObject();
		logJO.put("clinicId", clinicId);
		logJO.put("startTime", DateUtil.getLongDate(startTime));
		logJO.put("endTime", DateUtil.getLongDate(endTime));

		// Log log = new Log();
		// log.setEmployeeId(e.getEmployeeId());
		// log.setOperate(Constant.GET_CLINIC_REPORT.getName());
		// log.setOperateTime(new Timestamp(DateUtil.getNowLong()));
		// log.setContent(JSON.toJSONString(logJO));
		// lService.addEntity(log);

		return StringUtil.setResult(200, "获取成功", resultJA);
	}

	/**
	 * 获取本院自身支出细节
	 * 
	 * @param req
	 *            {clinicId,projectId,starTime(yyyy-MM-dd),endTime(yyyy-MM- dd)}
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getClinicSelfCostDetail", produces = "text/json;charset=utf-8")
	public String getClinicSelfCostDetail(WebRequest req, HttpSession session) {
		// 获取数据
		int clinicId = Integer.parseInt(req.getParameter("clinicId"), 10);
		int projectId = Integer.parseInt(req.getParameter("projectId"), 10);
		Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
		Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

		// TODO 验证数据合法性
		Employee e = (Employee) session.getAttribute("employee");
		if (e == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(e.getPositionId(), Constant.GET_REPORT.getName(), 3)) {
			return StringUtil.setResult(402, "权限不足", "");
		}

		JSONArray resultJA = new JSONArray();
		Project pj = pjService.getById(projectId);
		Clinic clinic = cService.getByid(clinicId);

		// [{employeeId,employeeCode,type,name,number,costPrice}]
		resultJA = bService.getClinicSelfCostDetail(pj, clinic, startTime, endTime);

		// 记录日志
		// JSONObject logJO = new JSONObject();
		// logJO.put("clinicId", clinicId);
		// logJO.put("projectId", projectId);
		// logJO.put("startTime", DateUtil.getLongDate(startTime));
		// logJO.put("endTime", DateUtil.getLongDate(endTime));
		//
		// Log log = new Log();
		// log.setEmployeeId(e.getEmployeeId());
		// log.setOperate(Constant.GET_SELF_COSTREPORT.getName());
		// log.setOperateTime(new Timestamp(DateUtil.getNowLong()));
		// log.setContent(JSON.toJSONString(logJO));
		// lService.addEntity(log);

		return StringUtil.setResult(200, "获取成功", resultJA);
	}

	/**
	 * 获取本院的分院支出细节
	 * 
	 * @param req
	 *            {clinicId,projectId,starTime(yyyy-MM-dd),endTime(yyyy-MM- dd)}
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getClinicOtherCostDetail", produces = "text/json;charset=utf-8")
	public String getClinicOtherCostDetail(WebRequest req, HttpSession session) {
		// 获取数据
		int clinicId = Integer.parseInt(req.getParameter("clinicId"), 10);
		int projectId = Integer.parseInt(req.getParameter("projectId"), 10);
		Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
		Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

		// TODO 验证数据合法性
		Employee e = (Employee) session.getAttribute("employee");
		if (e == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(e.getPositionId(), Constant.GET_REPORT.getName(), 3)) {
			return StringUtil.setResult(402, "权限不足", "");
		}

		JSONArray resultJA = new JSONArray();
		Project pj = pjService.getById(projectId);
		Clinic clinic = cService.getByid(clinicId);

		// [{clinicId,clinicName,clinicCost,employeeId,employeeCode,employeeName,employeeCost,number,totalCost}]
		resultJA = bService.getClinicOtherCostDetail(pj, clinic, startTime, endTime);

		// 记录日志
		// JSONObject logJO = new JSONObject();
		// logJO.put("clinicId", clinicId);
		// logJO.put("projectId", projectId);
		// logJO.put("startTime", DateUtil.getLongDate(startTime));
		// logJO.put("endTime", DateUtil.getLongDate(endTime));
		//
		// Log log = new Log();
		// log.setEmployeeId(e.getEmployeeId());
		// log.setOperate(Constant.GET_OTHER_COSTREPORT.getName());
		// log.setOperateTime(new Timestamp(DateUtil.getNowLong()));
		// log.setContent(JSON.toJSONString(logJO));
		// lService.addEntity(log);

		return StringUtil.setResult(200, "获取成功", resultJA);
	}

	/**
	 * 
	 * @param req
	 * @param session
	 * @return 
	 *         [{clinicName,departmentName,doctorId,doctorCode,doctorName,price,number
	 *         ,createTime}]
	 */
	@ResponseBody
	@RequestMapping(value = "/getClinicTreatmentDetail", produces = "text/json;charset=utf-8")
	public String getClinicTreatmentDetail(WebRequest req, HttpSession session) {
		Employee e = (Employee) session.getAttribute("employee");
		if (e == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(e.getPositionId(), Constant.GET_REPORT.getName(), 3)) {
			return StringUtil.setResult(402, "权限不足", "");
		}

		// 获取数据
		int clinicId = Integer.parseInt(req.getParameter("clinicId"), 10);
		int projectId = Integer.parseInt(req.getParameter("projectId"), 10);
		Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
		Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

		Project pj = pjService.getById(projectId);
		Clinic clinic = cService.getByid(clinicId);

		JSONArray resultJA = bService.getClinicTreatmentDetail(pj, clinic, startTime, endTime);
		// 记录日志
		// JSONObject logJO = new JSONObject();
		// logJO.put("clinicId", clinicId);
		// logJO.put("projectId", projectId);
		// logJO.put("startTime", DateUtil.getLongDate(startTime));
		// logJO.put("endTime", DateUtil.getLongDate(endTime));
		//
		// Log log = new Log();
		// log.setEmployeeId(e.getEmployeeId());
		// log.setOperate(Constant.GET_TEATMENT_REPORT.getName());
		// log.setOperateTime(new Timestamp(DateUtil.getNowLong()));
		// log.setContent(JSON.toJSONString(logJO));
		// lService.addEntity(log);

		return StringUtil.setResult(200, "获取成功", resultJA);
	}

	/**
	 * 获取科室治疗项目总报表
	 * 
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDepartmentTreatmentReport", produces = "text/json;charset=utf-8")
	public String getDepartmentTreatmentReport(WebRequest req, HttpSession session) {
		// 获取数据
		int departmentId = Integer.parseInt(req.getParameter("departmentId"), 10);
		Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
		Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

		// TODO 验证数据合法性
		Employee e = (Employee) session.getAttribute("employee");
		if (e == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(e.getPositionId(), Constant.GET_REPORT.getName(), 2)) {
			return StringUtil.setResult(402, "权限不足", "");
		}

		// 获取科室[stratTime,endTime]内开的治疗项目
		List<Project> pjList = pjService.getDepartmentTreatmentProject(departmentId, startTime,
				endTime);
		// System.out.println("getDepartmentTreatmentReport.pjList : "+JSON.toJSONString(pjList,true));

		Department dpm = dpmService.getByid(departmentId);
		JSONArray resultJA = bService.getDepartmentTreatmentProjectReport(pjList, dpm, startTime,
				endTime);

		// 记录日志
		// JSONObject logJO = new JSONObject();
		// logJO.put("departmentId", departmentId);
		// logJO.put("startTime", DateUtil.getLongDate(startTime));
		// logJO.put("endTime", DateUtil.getLongDate(endTime));
		//
		// Log log = new Log();
		// log.setEmployeeId(e.getEmployeeId());
		// log.setOperate(Constant.GET_REPORT.getName());
		// log.setOperateTime(new Timestamp(DateUtil.getNowLong()));
		// log.setContent(JSON.toJSONString(logJO));
		// lService.addEntity(log);

		return StringUtil.setResult(200, "操作成功", resultJA);
	}

	/**
	 * 获取科室治疗项目自身支出报表
	 * 
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDepartmentSelfCostDetail", produces = "text/json;charset=utf-8")
	public String getDepartmentSelfCostDetail(WebRequest req, HttpSession session) {
		// 检测权限
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService
				.isPermisionEnough(operator.getPositionId(), Constant.GET_REPORT.getName(), 2)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		try {
			// 获取数据
			int departmentId = Integer.parseInt(req.getParameter("departmentId"), 10);
			int projectId = Integer.parseInt(req.getParameter("projectId"), 10);
			Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
			Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

			Project pj = pjService.getById(projectId);
			Department dpm = dpmService.getByid(departmentId);
			// [{employeeId,employeeCode,type,name,number,costPrice}]
			JSONArray resultJA = bService.getDepartmentSelfCostDetail(pj, dpm, startTime, endTime);

			if (resultJA == null || resultJA.size() == 0) {
				return StringUtil.setResult(414, "数据为空", "");
			}
			return StringUtil.setResult(200, "操作成功", resultJA);

		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}

	/**
	 * 获取科室其他科室支出报表
	 * 
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDepartmentOtherCostDetail", produces = "text/json;charset=utf-8")
	public String getDepartmentOtherCostDetail(WebRequest req, HttpSession session) {
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService
				.isPermisionEnough(operator.getPositionId(), Constant.GET_REPORT.getName(), 2)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		try {
			// 获取数据
			int departmentId = Integer.parseInt(req.getParameter("departmentId"), 10);
			int projectId = Integer.parseInt(req.getParameter("projectId"), 10);
			Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
			Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

			Project pj = pjService.getById(projectId);
			Department dpm = dpmService.getByid(departmentId);
			// [{clinicId,clinicName,clinicCost,employeeId,employeeCode,employeeName
			// ,employeeCost,number,totalCost}]
			JSONArray resultJA = bService.getDepartmentOtherCostDetail(pj, dpm, startTime, endTime);

			if (resultJA == null || resultJA.size() == 0) {
				return StringUtil.setResult(414, "数据为空", "");
			}
			return StringUtil.setResult(200, "操作成功", resultJA);

		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}

	/**
	 * 获取可是 治疗项目细节报表
	 * 
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDepartmentTreatmentDetail", produces = "text/json;charset=utf-8")
	public String getDepartmentTreatmentDetail(WebRequest req, HttpSession session) {
		// 检测权限
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService
				.isPermisionEnough(operator.getPositionId(), Constant.GET_REPORT.getName(), 2)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		try {
			// 获取数据
			int departmentId = Integer.parseInt(req.getParameter("departmentId"), 10);
			int projectId = Integer.parseInt(req.getParameter("projectId"), 10);
			Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
			Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

			Project pj = pjService.getById(projectId);
			Department dpm = dpmService.getByid(departmentId);
			// [{departmentId,departmentName,doctorCode,doctorName,doctorId,price,billNumber}]
			JSONArray resultJA = bService.getDepartmentTreatmentDetail(pj, dpm, startTime, endTime);

			if (resultJA == null || resultJA.size() == 0) {
				return StringUtil.setResult(414, "数据为空", "");
			}
			return StringUtil.setResult(200, "操作成功", resultJA);

		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}
	
	/**
	 * 获取门诊费用项目报表
	 * 
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getClinicFundReport_new", produces = "text/json;charset=utf-8")
	public String getClinicFundReport_new(WebRequest req, HttpSession session) {
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(operator.getPositionId(), Constant.GET_REPORT.getName(), 3)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		
		try {
			int clinicId = Integer.parseInt(req.getParameter("clinicId"),10);
			int type = Integer.parseInt(req.getParameter("type"), 10);
			long datetime = DateUtil.getShortDateLong(req.getParameter("datetime"));
			
			JSONArray resultJA = rpService.getClinicFundReport(clinicId, type, datetime);
			return StringUtil.setResult(200, "获取成功", resultJA);
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数有误", "");
		}
		
	}
	
	/**
	 * 获取门诊费用项目报表
	 * 
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getClinicFundReport", produces = "text/json;charset=utf-8")
	public String getClinicFundReport(WebRequest req, HttpSession session) {
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService
				.isPermisionEnough(operator.getPositionId(), Constant.GET_REPORT.getName(), 3)) {
			return StringUtil.setResult(402, "权限不足", "");
		}

		try {
			int clinicId = Integer.parseInt(req.getParameter("clinicId"), 10);
			Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
			Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

			// 数据库操作
			Clinic clinic = cService.getByid(clinicId);
			// [clinicId,clinicName,projectId,projectName,type,price]
			JSONArray resultJA = bService.getClinicFundProjectReport(clinic, startTime, endTime);
			if (resultJA == null || resultJA.size() == 0) {
				return StringUtil.setResult(414, "数据为空", "");
			}
			return StringUtil.setResult(200, "操作成功", resultJA);
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}

	/**
	 * 获取门诊费用项目细节报表
	 * 
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getClinicFundDetail", produces = "text/json;charset=utf-8")
	public String getClinicFundDetail(WebRequest req, HttpSession session) {
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService
				.isPermisionEnough(operator.getPositionId(), Constant.GET_REPORT.getName(), 3)) {
			return StringUtil.setResult(402, "权限不足", "");
		}

		try {
			int clinicId = Integer.parseInt(req.getParameter("clinicId"), 10);
			int projectId = Integer.parseInt(req.getParameter("projectId"), 10);
			Long startTime = DateUtil.getShortDateLong(req.getParameter("startTime"));
			Long endTime = DateUtil.getShortDateLong(req.getParameter("endTime"));

			// 数据库操作
			Clinic clinic = cService.getByid(clinicId);
			// [operatorId,operatorCode,operatorName,price,dateTime]
			JSONArray resultJA = bService.getClinicFundProjectDetail(clinic, projectId, startTime,
					endTime);
			if (resultJA == null || resultJA.size() == 0) {
				return StringUtil.setResult(414, "数据为空", "");
			}
			return StringUtil.setResult(200, "操作成功", resultJA);
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}

	/**
	 * 格式化整合首页所有数据
	 * 
	 * @param clinic
	 * @param year
	 * @return
	 */
	private JSONObject getIndexReportResult(Clinic clinic, String date) {
		JSONObject resultJO = new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("MM月");
		List<Double> profitList = new ArrayList<Double>();
		List<Double> incomeList = new ArrayList<Double>();
		List<Double> costList = new ArrayList<Double>();
		List<String> dateList = new ArrayList<String>();
		
		Bill firstBill = bService.getClinicFirstBill(clinic);
		Calendar firstBillCalendar = Calendar.getInstance();
		if(firstBill == null) {
			firstBillCalendar.setTime(new Date());//获取最早的单据的时间
		}else {
			firstBillCalendar.setTime(new Date(firstBill.getDatetime().getTime()));//获取最早的单据的时间
		}
			
		
		String startTime = DateUtil.getMonthFirstDay(date);
		String endTime = DateUtil.getMonthLastDay(date);
		
		for(int i = 0;i < 6;i++) {
			long startTimeLong =  DateUtil.getShortDateLong(startTime);
			long endTimeLong = DateUtil.getShortDateLong(endTime);
			
			Calendar endCalendar = Calendar.getInstance();
			endCalendar.setTime(new Date(endTimeLong));
			//System.out.println("firstBillCalendar.compareTo(startCalendar) : "+firstBillCalendar.compareTo(endCalendar));
			if(firstBillCalendar.compareTo(endCalendar) > 0) {
				break;
			}
			
			//获取数据库数据
			JSONObject reportJO = getIndexReportDataDetail(clinic,startTimeLong,endTimeLong);
			profitList.add(0,reportJO.getDouble("profit"));
			incomeList.add(0,reportJO.getDouble("income"));
			costList.add(0,reportJO.getDouble("cost"));
			dateList.add(0,sdf.format(endCalendar.getTime()));
			
			//重置为上一个月
			startTime = DateUtil.minusMonth(startTime);
			endTime = DateUtil.minusMonth(endTime);
		}

		resultJO.put("profit", profitList);
		resultJO.put("income", incomeList);
		resultJO.put("cost", costList);
		resultJO.put("date", dateList);

		return resultJO;
	}

	/**
	 * 获取首页数据每个月的收入、支出、利润
	 * 
	 * @param clinic
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	private JSONObject getIndexReportDataDetail(Clinic clinic, long startTime, long endTime) {
		// [clinicId,clinicName,projectId,projectName,type,price]
		JSONArray reportJA = bService.getClinicFundProjectReport(clinic, startTime, endTime);
		double profit = 0.0;
		double income = 0.0;
		double cost = 0.0;
		for (int i = 0; i < reportJA.size(); i++) {
			JSONObject reportJO = reportJA.getJSONObject(i);
			if (reportJO.get("type").equals("收入")) {
				income += (double) reportJO.get("price");
			}
			if (reportJO.get("type").equals("支出")) {
				cost -= (double) reportJO.get("price");
			}
		}
		profit = income + cost;
		JSONObject resultJO = new JSONObject();
		resultJO.put("profit", DoubleUtil.parseDoubleTwo(profit));
		resultJO.put("income", DoubleUtil.parseDoubleTwo(income));
		resultJO.put("cost", DoubleUtil.parseDoubleTwo(cost));
		// System.out.println("getIndexReportData.resultJO : " +
		// resultJO.toJSONString());
		return resultJO;
	}
}
